﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Truextend.Common.Data;
using Truextend.Training.Model.Entities;
using Truextend.Training.Business;

namespace Truextend.Training.Business.Tests
{
    [TestClass]
    public class CourseManagerTests: BaseTestClass
    {
        [Priority(2), TestMethod]
        public void AddCourseManagerTest()
        {
            int beforeInset = 0;
            int afterInsert = 0;
            CourseManager courseManager = null;
            ExecuteManager(uow =>
            {
                courseManager = new CourseManager(uow);
                Course course1 = new Course
                {
                    Name = "Course 1",
                    Category = "Programming",
                    DirectedTo = "Developers",
                    Prerequisites = "Course x",
                    Contents = "blah blah...."
                };

                beforeInset = courseManager.GetCount();
                courseManager.AddCourse(course1);
            }, () =>
            {
                afterInsert = courseManager.GetCount();
            }
                );
            Assert.AreEqual(beforeInset + 1, afterInsert);
        }

        [TestMethod]
        public void GetCourseManagerTest()
        {
            Course courseRecovered =  null;
            CourseManager courseManager = null;
            ExecuteManager(uow =>
            {
                courseManager = new CourseManager(uow);
                courseRecovered = courseManager.GetCourseById( new Repository<Course>(uow).DoQuery().First().CourseId );
            });

            Assert.IsNotNull(courseRecovered);
        }

        [TestMethod]
        public void EditCourseManagerTest()
        {
            Course courseRecovered = null;
            Course courseEdit = null;
            CourseManager courseManager = null;
            
            ExecuteManager(uow =>
            {
                courseManager = new CourseManager(uow);
                courseEdit = courseManager.GetCourseById(new Repository<Course>(uow).DoQuery().First().CourseId);
                courseRecovered = new Course();
                courseRecovered.CourseId = courseEdit.CourseId;
                courseRecovered.Name = courseEdit.Name;
                courseRecovered.Category = courseEdit.Category;
                courseRecovered.DirectedTo = courseEdit.DirectedTo;
                courseRecovered.Prerequisites = courseEdit.Prerequisites;
                courseRecovered.Contents = courseEdit.Contents;

                courseEdit.Name = "EDIT NAME";
                courseEdit.Category = "EDIT CATEGORY";
            });

            Assert.AreNotEqual(courseEdit, courseRecovered);
        }
        

        [TestMethod]
        public void DeleteCourseManagerTest()
        {
            int beforeInset = 0;
            int afterInsert = 0;
            CourseManager courseManager = null;
            ExecuteManager(uow =>
            {
                courseManager = new CourseManager(uow);
                long idDelete = courseManager.GetAllCourses().ToList().Last().CourseId;
                Course courseRecovered = courseManager.GetCourseById(idDelete);

                beforeInset = courseManager.GetCount();
                courseManager.DeleteCourse(courseRecovered.CourseId);

            }, () =>
                {
                    afterInsert = courseManager.GetCount();
                }
            );

            Assert.AreNotEqual(beforeInset, afterInsert);
        }

    }
}
